﻿using System.ComponentModel;

namespace XamlMvp.Modelling
{
    public class Editable<T> : IEditableObject where T : ICloneable<T>, ISynchronise<T>
    {
        readonly T current;        
        T original;        

        public Editable(T current)
        {
            this.current = current;           
        }

        public Editable(T current, T original) : this(current)
        {
            this.original = original;
        }

        public void BeginEdit()
        {
            if (original == null)
            {
                original = current.Clone();                
            }
        }

        public void CancelEdit()
        {
            if (original != null)
            {
                current.CopyFrom(original);
                EndEdit();
            }
        }

        public void EndEdit() 
        {
            original = default(T);
        }

        public T Current { get { return current; } }
        public T Original { get { return original; } }
        public bool IsEditing { get { return original != null; } }
        
    }
}
